"use strict";
const { Service } = require("egg");
const p = require("path");
const fs = require("fs-extra");

class NotifiService extends Service {
  async getList(params) {
    let { limit, offset } = params;
    try {
      let ret = await this.ctx.model.Notifi.findAndCountAll({
        limit,
        offset,
        order: [
          ['created_at', 'DESC'],
        ]
      });
      return { data: ret, msg: "获取通知列表成功" };
    } catch (e) {
      console.log(e);
      return { msg: "获取通知列表失败", code: 400 };
    }
  }
  async getCount() {
    try {
      let count = await this.ctx.model.Notifi.count({
        where: {
          is_read: false,
        },
      });
      return { data: { count }, msg: "获取通知列表成功" };
    } catch (e) {
      console.log(e);
      return { msg: "获取通知数量失败", code: 400 };
    }
  }
  async readNotifi(params) {
    let { list } = params;
    for (let itm of list) {
      await this.ctx.model.Notifi.update(
        { is_read: true },
        {
          where: { id: itm },
        }
      );
    }
  }
}

module.exports = NotifiService;
